package utils

import "time"

// DateDiff 求两个时间相隔几天
func DateDiff(before int64, after int64) int {
	t1 := before
	t2 := after
	time1 := time.Unix(t1, 0)
	time2 := time.Unix(t2, 0)
	day := ((t2 - int64(time2.Hour()*60*60) - int64(time2.Minute()*60) - int64(time2.Second())) -
		(t1 - int64(time1.Hour()*60*60) - int64(time1.Minute()*60) - int64(time1.Second()))) / (60 * 60 * 24)
	return int(day)
}

// 一个星期的第一天
func GetFirstDateOfWeek(now time.Time) time.Time {
	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}
	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	return weekStartDate
}

// 计算给定日期所在周的指定星期几的日期
func GetWeekdayDate(date time.Time, targetWeekday time.Weekday) time.Time {
	currentWeekday := date.Weekday() // 获取当前日期是星期几 (0=Sunday, ..., 6=Saturday)
	offset := int(targetWeekday) - int(currentWeekday)
	return date.AddDate(0, 0, offset) // 调整到目标日期
}
